<?php
require_once('Zend/Controller/Action.php');


class ResumeController extends Zend_Controller_Action {

     

    public function preDispatch(){

        $auth = Zend_Auth::getInstance();
        if(!$auth->hasIdentity()){
             
            Message::setMessage('Please login before you can take this action.', 'error');

            $this->_redirect('/user/login');

        }elseif($this->getRequest()->getActionName() != 'index'
               && $this->getRequest()->getActionName() != 'create'){


            $resume = new Resume();
            
            if(!$resume->isResumeOwner($this)
            && $auth->getIdentity()->id != 1){

                require_once('exceptions/Exception_Acl_No_Perm.php');
                throw new Exception_Acl_No_Perm('You have no permission to take this action.');
            }
        }

    }

    public function init(){

        Zend_Loader::loadClass('Resume');
        $this->view->user = Zend_Auth::getInstance()->getIdentity();

    }

    public function indexAction(){

        $this->view->title = "My Resumes";
        $resume = new Resume();
        $auth = Zend_Auth::getInstance();
        (int)$user_id = $auth->getIdentity()->id;
        $rows = $resume->fetchAll('user_id = '.$user_id);
        if($rows->count() <= 0)
        Message::setMessage('No resume has been found, create a new resume.');

        $this->view->resumes = $rows;
         


    }

    public function createAction(){

        $this->view->title = "Create New Resume";
        $this->view->action ="create";
        $this->view->buttonText = "Create";

        $resume = new Resume();
        //Insert data of form into database.
        if($this->_request->isPost()){

            $resume->createResume();
            //$this->_redirect('/resume/index');
        }else{


            $this->view->resume = $resume->createRow();
        }
    }

    public function viewAction(){

        $this->view->title = "View Resume";
         
        $resume = new Resume();
        $row = $resume->fetchResume($this);

        $this->view->content = $row->content;
        $this->view->id = $row->id;

    }

    public function editAction(){
         
        $this->view->title = 'Edit Resume';
        $this->view->action = 'edit';
        $this->view->buttonText = 'Edit';

        $resume = new Resume();
        if($this->_request->isPost()){
             
            $id = (int)$this->_request->getPost('id');
            $resume->updateResume($id);
            $this->_redirect('/resume/view/id/'.$id);
        }else{

            $this->view->resume = $resume->fetchResume($this);

            $this->view->content = $this->view->resume->content;

        }
    }

    public function duplicateAction(){

        $id = (int)$this->_request->getParam('id');
        $resume = new Resume();
        $row = $resume->fetchRow('id = '.$id);
        $data = array(
                'title' => $row->title,
                'user_id' => Zend_Auth::getInstance()->getIdentity()->id,
                'content' => $row->content,
                'created' => time(),
                'edited' => time(), 
        );
        $resume->insert($data);
        $this->_redirect('/resume/index');
    }

    public function deleteAction(){

        $this->view->title = 'Delete Resume';
        if(!$this->_request->isPost()){
             
            $resume = new Resume();
            $this->view->resume = $resume->fetchResume($this);
        }


        if($this->_request->isPost()){

            if($this->_request->getPost('confirm') == 'yes'){
                $id = (int)$this->_request->getPost('id');
                $resume = new Resume();
                $resume->delete('id = '.$id);

            }

            $this->_redirect('/resume/index');

        }
    }

    public function downloadAction(){

        $id = (int)$this->_request->getParam('id');
        if($this->_request->getParam('type') == 'pdf' && $id > 0){

             
            $resume = new Resume();
            $resume->outputResumePdf($this);
        }
    }

    public function sendAction(){

         

        $this->view->title = 'Send Resume';
        $this->view->messages = array();
        $resume = new Resume();
        $row = $resume->fetchResume($this);

        $this->view->resume = $row;

        //Process form/Post method
        if($this->_request->isPost()){

            //Filter Post input
            Zend_Loader::loadClass('PostFilter');
            PostFilter::filter();


            //Validate Email Address
            Zend_Loader::loadClass('Zend_Validate_Regex');
            $emailValidator = new Zend_Validate_Regex('/^[A-Za-z0-9.-_]+@[A-Za-z0-9.-]+\.[a-z]{2,4}$/');
            $emailValidator->setMessage('Valid email address requried.');

            $valid = 1;
            for($i=1;$i<=5;$i++){
                if($_POST['addr_'.(string)$i] != ''){
                    $valid *= (int)$emailValidator->isValid($_POST['addr_'.$i]);
                }
            }
            if($valid == 0){

                $this->view->messages += $emailValidator->getMessages();

            }
            if($valid == 1){


                //Send resume by email
                for($i=1;$i<=5;$i++){

                    if(!empty($_POST['addr_'.$i])){
                         
                        $addr = $_POST['addr_'.$i];


                        Zend_Loader::loadClass('Zend_Mail');
                        $mail = new Zend_Mail();
                        $mail
                        ->setFrom($row->content['email'], $row->content['first_name'].' '.$row->content['surname'] )
                        ->setReturnPath($row->content['email'])
                        ->addTo($addr)
                        ->setSubject($_POST['subject'])
                        ->setBodyHtml($this->view->render('/resume/_mail.phtml'),'us-ascii','7bit')
                        ->send();
                    }
                }

            }


        }else{

            //If the method is "GET"
        }
    }
}
?>